#!/bin/bash

# Copyright 2014 Intel Corporation, All Rights Reserved.

# Licensed under the Apache License, Version 2.0 (the"License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

#  http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.


function gen_tar_file() {
    local ssh_user=$1
    local vsmapp_ip=$2
    local os_controller_host=$3
    local install_file=$4

    TEMP=`mktemp`; rm -rf $TEMP; mkdir -p $TEMP/prepools
    cp -rf /etc/vsm/prepools/* $TEMP/prepools/
    cp -rf /etc/ceph/ceph.conf $TEMP/prepools/lib/
    sed -i "s,keyring = /etc/ceph/keyring.admin,,g" $TEMP/prepools/lib/ceph.conf
    cd $TEMP/prepools


cat <<"EOF" > localrc
AUTH_CINDER_USER=%AUTH_CINDER_USER%
AUTH_CINDER_POOL=%AUTH_CINDER_POOL%
AUTH_CINDER_UUID=%AUTH_CINDER_UUID%
AUTH_GLANCE_USER=%AUTH_GLANCE_USER%
AUTH_GLANCE_POOL=%AUTH_GLANCE_POOL%
BACKEND_LIST=%BACKEND_LIST%
TYPE_LIST=%TYPE_LIST%
EOF

    sed -i "s,%AUTH_CINDER_USER%,$AUTH_CINDER_USER,g" localrc
    sed -i "s,%AUTH_CINDER_POOL%,$AUTH_CINDER_POOL,g" localrc
    sed -i "s,%AUTH_CINDER_UUID%,$AUTH_CINDER_UUID,g" localrc
    sed -i "s,%AUTH_GLANCE_USER%,$AUTH_GLANCE_USER,g" localrc
    sed -i "s,%AUTH_GLANCE_POOL%,$AUTH_GLANCE_POOL,g" localrc

    if [[ ${#BACKEND_LIST} -gt 0 ]]; then
        sed -i "s/%BACKEND_LIST%/$BACKEND_LIST/g" localrc
        sed -i "s/%TYPE_LIST%/$TYPE_LIST/g" localrc
    else
        sed -i "/BACKEND_LIST/d" localrc
        sed -i "/TYPE_LIST/d" localrc
    fi

    cp -rf $install_file install
    chmod +x install
    rm -rf main
    rm -rf server

    #-----------------------------------
    # Gent tar file run in OpenStack node.
    #-----------------------------------

    cd $TEMP
    tar zcf prepools.tar.gz prepools
    su -s /bin/bash -c "exec ssh $os_controller_host mkdir -p $TEMP" $ssh_user
    su -s /bin/bash -c "exec ssh $os_controller_host ssh $vsmapp_ip mkdir -p $TEMP" $ssh_user
    su -s /bin/bash -c "exec scp -pr prepools.tar.gz $os_controller_host:$TEMP" $ssh_user
    su -s /bin/bash -c "exec ssh $os_controller_host scp $TEMP/prepools.tar.gz $vsmapp_ip:$TEMP" $ssh_user
    su -s /bin/bash -c "exec ssh $os_controller_host ssh $vsmapp_ip tar zxf $TEMP/prepools.tar.gz -C $TEMP" $ssh_user
    su -s /bin/bash -c "exec ssh $os_controller_host ssh $vsmapp_ip sudo bash +x $TEMP/prepools/install" $ssh_user
}
